package io;


import org.junit.Test;

import java.io.*;

/**
 * @description: 转换类的测试
 * 1、转换流
 *  InputStreamReader  字节的输入流转换为字符的输入流
 *  OutputStreamWriter 字符的输出流转换为字节的输出流
 *  -》都是属于字符体系的下的
 *  2、作用：提供字节和字符之间的转换
 *  3、解码：字节、字节数组 -》字符数组、字符串
 *  4、编码：字符数组、字符串-》字节、字节数组
 * @author: apple
 * @date:  2021/12/25 9:41 下午
 * @version 1.0
 **/
public class InputSteamReaderTest {

    @Test
    public void test1() throws IOException {

        FileInputStream in = new FileInputStream("test.txt");
        // 为什么写utf-8，取决于文件是什么字符集
        // 把字节流转换为字符流
        InputStreamReader reader = new InputStreamReader(in, "UTF-8");

        char[] buff = new char[1024];
        int len;
        while (((len = reader.read(buff)) != -1)) {
            String str = new String(buff, 0, len);
            System.out.println(str);
        }

        reader.close();
        in.close();
    }

    @Test
    public void test2() throws IOException {

        FileInputStream in = new FileInputStream("test.txt");
        FileOutputStream out = new FileOutputStream("test6.txt");

        InputStreamReader reader = new InputStreamReader(in,"UTF-8");
        OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");

        char[] buffers = new char[1024];
        int len;
        while (((len = reader.read(buffers)) != -1)) {
            writer.write(buffers,0,len);
        }
        writer.flush();

        writer.close();
        reader.close();
    }

}
